package com.atguigu.srb.core.controller.api;


import com.atguigu.common.exception.Assert;
import com.atguigu.common.result.R;
import com.atguigu.common.result.ResponseEnum;
import com.atguigu.common.util.RegexValidateUtils;
import com.atguigu.srb.base.util.JwtUtils;
import com.atguigu.srb.core.pojo.vo.LoginVO;
import com.atguigu.srb.core.pojo.vo.RegisterVO;
import com.atguigu.srb.core.pojo.vo.UserIndexVO;
import com.atguigu.srb.core.pojo.vo.UserInfoVO;
import com.atguigu.srb.core.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 用户基本信息 前端控制器
 * </p>
 *
 * @author TieZhu
 * @since 2021-06-10
 */
@Api(tags = "会员接口")
@RestController
@RequestMapping("/api/core/userInfo")
@Slf4j
public class UserInfoController {

    @Resource
    private UserInfoService userInfoService;
    @Resource
    private RedisTemplate redisTemplate;

    @ApiOperation("会员注册")
    @PostMapping("/register")
    public R resister(@RequestBody RegisterVO registerVO){
        String mobile=registerVO.getMobile();
        String code=registerVO.getCode();
        String password=registerVO.getPassword();
        //MOBILE_NULL_ERROR(-202, "手机号不能为空"),
        Assert.notEmpty(mobile, ResponseEnum.MOBILE_NULL_ERROR);
        //MOBILE_ERROR(-203, "手机号不正确"),
        Assert.isTrue(RegexValidateUtils.checkCellphone(mobile), ResponseEnum.MOBILE_ERROR);
        //PASSWORD_NULL_ERROR(-204, "密码不能为空"),

        Assert.notEmpty(password, ResponseEnum.PASSWORD_NULL_ERROR);
        //CODE_NULL_ERROR(-205, "验证码不能为空"),
        Assert.notEmpty(code, ResponseEnum.CODE_NULL_ERROR);

        String codeGen = (String) redisTemplate.opsForValue().get("srb:sms:code:" + mobile);

        Assert.equals(code,codeGen, ResponseEnum.CODE_ERROR);
        userInfoService.register(registerVO);
        return R.ok().message("注册成功");
    }

    @ApiOperation("会员登录")
    @PostMapping("/login")
    public R userLogin(@RequestBody LoginVO loginVO, HttpServletRequest request,HttpServletResponse response){

        String mobile=loginVO.getMobile();
        String pwd=loginVO.getPassword();
        System.out.println(mobile);
        System.out.println(pwd);
        Assert.notEmpty(mobile,ResponseEnum.MOBILE_NULL_ERROR);
        Assert.notEmpty(pwd,ResponseEnum.PASSWORD_NULL_ERROR);

        String ip=request.getRemoteAddr();
        UserInfoVO userInfoVO=userInfoService.login(loginVO,ip);
        //userInfoService.insertDate();
        log.info(userInfoVO.getToken());
        response.setHeader("token",userInfoVO.getToken());
        return R.ok().data("userInfo",userInfoVO);
    }
    @ApiOperation("校检令牌")
    @GetMapping("/checkToken")
    public R checkToken(HttpServletRequest request){
        String token=request.getHeader("token");
        boolean result= JwtUtils.checkToken(token);
        if (result){
            return R.ok();
        }else {
            return R.setResult(ResponseEnum.LOGIN_AUTH_ERROR);
        }
    }

    @ApiOperation("密码重置")
    @PostMapping("/reset")
    public R resetPassword(@RequestBody RegisterVO registerVO){
        String mobile=registerVO.getMobile();
        String code=registerVO.getCode();
        String password=registerVO.getPassword();
        //MOBILE_NULL_ERROR(-202, "手机号不能为空"),
        Assert.notEmpty(mobile, ResponseEnum.MOBILE_NULL_ERROR);
        //MOBILE_ERROR(-203, "手机号不正确"),

        Assert.isTrue(RegexValidateUtils.checkCellphone(mobile), ResponseEnum.MOBILE_ERROR);
        //PASSWORD_NULL_ERROR(-204, "密码不能为空"),

        Assert.notEmpty(password, ResponseEnum.PASSWORD_NULL_ERROR);
        //CODE_NULL_ERROR(-205, "验证码不能为空"),
        Assert.notEmpty(code, ResponseEnum.CODE_NULL_ERROR);

        String codeGen = (String) redisTemplate.opsForValue().get("srb:sms:code:" + mobile);

        Assert.equals(code,codeGen, ResponseEnum.CODE_ERROR);
        int flag=userInfoService.resetPwd(registerVO);
        if (flag<=0){
            return R.error().message("重置密码失败");
        }else {
            return R.ok().message("重置密码成功");
        }
    }
    @ApiOperation("校验手机号码是否已经存在")
    @GetMapping("/checkMobile/{mobile}")
    public boolean mobileCheck(@PathVariable String mobile){
        boolean res=userInfoService.checkMobile(mobile);
        return res;
    }
    @ApiOperation("获取个人空间用户信息")
    @GetMapping("/auth/getIndexUserInfo")
    public R getIndexUserInfo(HttpServletRequest request) {
        System.out.println("获取用户信息执行了");
        String token=request.getHeader("token");
        System.out.println(token);
        Long userId=JwtUtils.getUserId(token);
        log.info("id: "+userId);
        UserIndexVO userIndexVO=userInfoService.getIndexUserInfo(userId);
        System.out.println(userIndexVO.toString());
        return R.ok().data("userIndexVo",userIndexVO);
    }

}

